package com.innovations.inn.controller;

import com.innovations.common.annotation.Log;
import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.page.TableDataInfo;
import com.innovations.common.enums.BusinessType;
import com.innovations.common.utils.poi.ExcelUtil;
import com.innovations.framework.web.service.TokenService;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.service.IResearchInstitutionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 科研机构Controller
 *
 * @author dp
 * @date 2024-08-09
 */
@Api(tags = "科研机构管理")
@RestController
@RequestMapping("/inn/institution")
public class ResearchInstitutionController extends BaseController {
    @Autowired
    private IResearchInstitutionService researchInstitutionService;
    @Autowired
    TokenService tokenService;

    /**
     * 查询科研机构列表
     */
    @ApiOperation("查询科研机构列表")
    @PreAuthorize("@ss.hasPermi('inn:institution:list')")
    @GetMapping("/list")
    public TableDataInfo list(ResearchInstitution researchInstitution) {
        startPage();
        List<ResearchInstitution> list = researchInstitutionService.selectResearchInstitutionList(researchInstitution);
        return getDataTable(list);
    }

    /**
     * 导出科研机构列表
     */
    @PreAuthorize("@ss.hasPermi('inn:institution:export')")
    @Log(title = "科研机构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResearchInstitution researchInstitution) {
        List<ResearchInstitution> list = researchInstitutionService.selectResearchInstitutionList(researchInstitution);
        ExcelUtil<ResearchInstitution> util = new ExcelUtil<ResearchInstitution>(ResearchInstitution.class);
        util.exportExcel(response, list, "科研机构数据");
    }

    /**
     * 获取科研机构详细信息
     */
    @ApiOperation("获取科研机构详细信息")
    @PreAuthorize("@ss.hasPermi('inn:institution:query')")
    @GetMapping(value = "/{institutionId}")
    public AjaxResult getInfo(@PathVariable("institutionId") Long institutionId) {
        return success(researchInstitutionService.selectResearchInstitutionByInstitutionId(institutionId));
    }

    /**
     * 新增科研机构
     */
    @ApiOperation("新增科研机构")
    @PreAuthorize("@ss.hasPermi('inn:institution:add')")
    @Log(title = "科研机构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody ResearchInstitution researchInstitution) {
        Long userId = tokenService.getLoginUser(request).getUser().getUserId();
        researchInstitutionService.completeData(researchInstitution, userId, 1);
        return toAjax(researchInstitutionService.insertResearchInstitution(researchInstitution));
    }

    /**
     * 修改科研机构
     */
    @ApiOperation("修改科研机构")
    @PreAuthorize("@ss.hasPermi('inn:institution:edit')")
    @Log(title = "科研机构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(HttpServletRequest request, @RequestBody ResearchInstitution researchInstitution) {
        Long userId = researchInstitution.getUserId();
        if (userId == null) {
            userId = tokenService.getLoginUser(request).getUser().getUserId();
        }
        researchInstitutionService.completeData(researchInstitution, userId, 2);
        return toAjax(researchInstitutionService.updateResearchInstitution(researchInstitution));
    }

    /**
     * 删除科研机构
     */
    @ApiOperation("删除科研机构")
    @PreAuthorize("@ss.hasPermi('inn:institution:remove')")
    @Log(title = "科研机构", businessType = BusinessType.DELETE)
    @DeleteMapping("/{institutionIds}")
    public AjaxResult remove(@PathVariable Long[] institutionIds) {
        return toAjax(researchInstitutionService.deleteResearchInstitutionByInstitutionIds(institutionIds));
    }
}
